<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
  <head>
    <th:block th:include="include :: header('修改绘声绘影动态')" />
    <th:block th:include="include :: summernote-css" />
  </head>
  <body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
      <form
        class="form-horizontal m"
        id="form-graphic-edit"
        th:object="${djGraphicDynamic}"
      >
        <input name="id" th:field="*{id}" type="hidden" />
        <div class="form-group">
          <label class="col-sm-3 control-label is-required">动态标题：</label>
          <div class="col-sm-8">
            <input
              name="title"
              th:field="*{title}"
              class="form-control"
              type="text"
              maxlength="100"
              required
            />
          </div>
        </div>
        <!-- 发布者和头像字段已隐藏，后端自动获取当前登录用户信息 -->
        <div class="form-group">
          <label class="col-sm-3 control-label is-required">动态内容：</label>
          <div class="col-sm-8">
            <textarea
              name="content"
              th:field="*{content}"
              class="form-control"
              rows="10"
              required
            ></textarea>
          </div>
        </div>
      </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script type="text/javascript">
      var prefix = ctx + "hui/interact/graphic";
      $("#form-graphic-edit").validate({
        focusCleanup: true,
      });

      // 初始化富文本编辑器
      $("textarea[name='content']").summernote({
        height: 300,
        lang: "zh-CN",
        callbacks: {
          onImageUpload: function (files) {
            uploadImage(files[0], this);
          },
        },
        toolbar: [
          ["style", ["style"]],
          ["font", ["bold", "underline", "clear"]],
          ["color", ["color"]],
          ["para", ["ul", "ol", "paragraph"]],
          ["table", ["table"]],
          ["insert", ["link", "picture", "video"]],
          ["view", ["fullscreen", "codeview", "help"]],
        ],
      });

      // 图片上传处理
      function uploadImage(file, editor) {
        var formData = new FormData();
        formData.append("file", file);

        $.ajax({
          url: ctx + "common/upload",
          type: "POST",
          data: formData,
          processData: false,
          contentType: false,
          success: function (result) {
            if (result.code === 0) {
              $(editor).summernote("insertImage", result.url);
            } else {
              $.modal.alertError("图片上传失败：" + result.msg);
            }
          },
          error: function () {
            $.modal.alertError("图片上传失败");
          },
        });
      }

      function submitHandler() {
        if ($.validate.form()) {
          // 获取富文本内容
          var content = $("textarea[name='content']").summernote("code");
          var data = {
            id: $("input[name='id']").val(),
            title: $("input[name='title']").val(),
            content: content,
          };

          $.ajax({
            url: prefix + "/edit",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (result) {
              if (result.code == 0) {
                $.modal.msgSuccess(result.msg);

                // 调试并刷新父页面
                console.log("=== 编辑成功，准备刷新父页面 ===");
                console.log("window.parent:", window.parent);
                console.log(
                  "window.parent.loadDynamics:",
                  window.parent.loadDynamics
                );
                console.log("window.top:", window.top);
                console.log(
                  "window.top.loadDynamics:",
                  window.top ? window.top.loadDynamics : "top不存在"
                );

                // 尝试刷新父页面
                var refreshed = false;

                // 方式1: 父窗口
                if (window.parent && window.parent.loadDynamics) {
                  try {
                    window.parent.loadDynamics();
                    console.log("✓ 通过window.parent刷新成功");
                    refreshed = true;
                  } catch (e) {
                    console.error("✗ window.parent刷新失败:", e);
                  }
                }

                // 方式2: 顶层窗口
                if (!refreshed && window.top && window.top.loadDynamics) {
                  try {
                    window.top.loadDynamics();
                    console.log("✓ 通过window.top刷新成功");
                    refreshed = true;
                  } catch (e) {
                    console.error("✗ window.top刷新失败:", e);
                  }
                }

                // 方式3: 使用Ruoyi框架的刷新方法
                if (!refreshed) {
                  try {
                    // 尝试使用框架的表格刷新
                    if (window.parent.$ && window.parent.$.table) {
                      window.parent.$.table.refresh();
                      console.log("✓ 通过$.table.refresh刷新成功");
                      refreshed = true;
                    }
                  } catch (e) {
                    console.error("✗ $.table.refresh刷新失败:", e);
                  }
                }

                // 方式4: 重新加载父页面
                if (!refreshed) {
                  try {
                    window.parent.location.reload();
                    console.log("✓ 重新加载父页面");
                    refreshed = true;
                  } catch (e) {
                    console.error("✗ 重新加载父页面失败:", e);
                  }
                }

                if (!refreshed) {
                  console.warn("⚠️ 所有刷新方式都失败了");
                }

                // 关闭弹窗
                try {
                  var index = window.parent.layer.getFrameIndex(window.name);
                  window.parent.layer.close(index);
                  console.log("弹窗关闭成功");
                } catch (e) {
                  console.error("弹窗关闭失败:", e);
                }
              } else {
                $.modal.msgError(result.msg);
              }
            },
            error: function (xhr, status, error) {
              $.modal.msgError("提交失败，请重试");
            },
          });
        }
      }
    </script>
  </body>
</html>
